package middle

import (
	"back/constant"
	"github.com/gin-gonic/gin"
	"strings"
	"utilx/rox"
)

func CacheForce(handles ...func(ctx *gin.Context)) gin.HandlerFunc {
	return func(ctx *gin.Context) {
		for _, handle := range handles {
			handle(ctx)
		}
		_, b := ctx.GetQuery(constant.CacheForce)
		isAdmin := ctx.GetBool(constant.IsAdmin)
		if isAdmin && b {
			ctx.Set(constant.CacheForce, true)
		} else {
			ctx.Set(constant.CacheForce, false)
		}
	}
}
func PermissionType(skips ...func(ctx *gin.Context)) gin.HandlerFunc {
	return func(ctx *gin.Context) {
		for _, handle := range skips {
			handle(ctx)
		}
		b := ctx.GetBool(constant.IsAdmin)
		if !b {
			// 白名单
			if strings.Contains(ctx.Request.URL.Path, "search") ||
				strings.Contains(ctx.Request.URL.Path, "login") {
				ctx.Next()
				return
			}
			rox.HandleErr[any](ctx, "err permission", nil)
			return
		}
		ctx.Next()
	}
}
